import config from '../config'
import axios from 'axios'

const baseUrl: string = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro

class HttpRequest {
  baseUrl: string;

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl;
  }

  getInsideConfig() {
    const config = {
      baseUrl: this.baseUrl,
      header: {}
    }
    return config;
  }

  intercetors(instance) {
    axios.interceptors.request.use((config) => {
      return config;
    }, (err) => {
      return Promise.reject(err);
    });

    axios.interceptors.response.use((res) => {
      console.log('inter res: ', res);
      return res;
    }, (err) => {
      console.log('inter err: ', err);
      return Promise.reject(err);
    });
  }

  request(options) {
    const instance = axios.create();
    options = { ...this.getInsideConfig(), ...options }
    console.log('拦截请求：');
    this.intercetors(instance);
    return instance(options);
  }
}

export default new HttpRequest(baseUrl);